 import java.net.*;
    import java.io.*;

    public class FileServerPC {

    static String remoteIPaddress = "192.168.76.208";
    static String filename;
    String path;
    static File selectedfile = null;
    static ServerSocket servsock = null;
    static Socket sock = null;
    static FileInputStream fis;
    static BufferedInputStream bis;
    static DatagramSocket theSocket = null;
    static DatagramPacket theOutput;
    static OutputStream os;
    byte[] mybytearray;
    static double nosofpackets;
    static int packetsize = 1024;

    public FileServerPC() {
        try {
            servsock = new ServerSocket(1500);
            theSocket = new DatagramSocket(9999);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {
        FileServerPC fs = new FileServerPC();

        selectedfile = new File("C:\\flower.jpg");
        filename = selectedfile.getName();


        Runnable runnable = new Runnable() {
            @Override
            public void run() {

        try {
            System.out.println("Waiting...");

            sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);
            fis = new FileInputStream(selectedfile);
            System.out.println((int) selectedfile.length());
            long length = selectedfile.length();
            System.out.println("LENGTH: " + length);
            nosofpackets = Math
                    .ceil(((int) selectedfile.length()) / packetsize);
            // progressBar.setValue(50);
            bis = new BufferedInputStream(fis);
            String message = length + "`~`" + filename;
            byte[] data = message.getBytes();
            theOutput = new DatagramPacket(data, data.length,
                    InetAddress.getByName(remoteIPaddress),8888);
            theSocket.send(theOutput);
            byte[] newbytearray = new byte[packetsize];

            int dur = (int) (selectedfile.length());
            int dur1 = dur / 100;
            int counter = 0;

            System.out.println("duration: " + dur + "nosofpackets: "
                    + nosofpackets + "dur1: " + dur1);
            int val = 0;
            for (int i = 0; i <= nosofpackets ; i++) {
                os = sock.getOutputStream();
                bis.read(newbytearray, 0, newbytearray.length);
                os.write(newbytearray, 0, newbytearray.length);
                counter = counter + newbytearray.length;

                val = counter / dur1;
                System.out.println(val);
                os.flush();
            }
            System.out.println(val);
            os.flush();
            os.close();
            sock.close();
            theSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
            }
        };

        new Thread(runnable).start();
    }

}


